import 'dart:collection';

import 'package:flutter/cupertino.dart';
import 'package:flutter_wan_android/entity/article_entity.dart';
import 'package:flutter_wan_android/entity/home_banner_entity.dart';
import 'package:flutter_wan_android/entity/paging_article_entity.dart';
import 'package:flutter_wan_android/entity/project_tree_entity.dart';
import 'package:flutter_wan_android/entity/search_hot_key_entity.dart';
import 'package:flutter_wan_android/entity/structure_category_entity.dart';
import 'package:flutter_wan_android/entity/structure_nav_entity.dart';
import 'package:flutter_wan_android/entity/wan_android_user_entity.dart';
import 'package:flutter_wan_android/http/http_constant.dart';
import 'package:flutter_wan_android/http/http_utils.dart';

class WanAndroidRepository {
  /// 轮播
  static Future<List<HomeBannerEntity>> fetchBanners() {
    return HttpUtils<List<HomeBannerEntity>>().getRequest(HttpConstant.URL_BANNER).then((value) => value.data);
  }

  /// 置顶文章
  static Future<List<ArticleEntity>> fetchTopArticles() {
    return HttpUtils<List<ArticleEntity>>().getRequest(HttpConstant.URL_TOP_ARTICLES).then((value) => value.data);
  }

  /// 文章列表
  static Future<List<ArticleEntity>> fetchArticles(int pageNum, {int cid}) async {
    await Future.delayed(Duration(seconds: 1)); //增加动效
    String articleURL = 'article/list/$pageNum/json';
    HashMap<String, String> params;
    if (cid != null) {
      params = HashMap.from({"cid": cid});
    }

    return HttpUtils<PagingArticleEntity>()
        .getRequest(articleURL, queryParameters: params)
        .then((value) => value.data?.datas);
  }

  /// 登录
  /// dio 添加了拦截器 设置了自动cookie.
  static Future login(String username, String password) {
    return HttpUtils<WanAndroidUserEntity>().postRequest(HttpConstant.USER_LOGIN, queryParameters: {
      'username': username,
      'password': password,
    }).then((value) => value.data);
  }

  /// 注册
  static Future register(String username, String password, String rePassword) {
    return HttpUtils<WanAndroidUserEntity>().postRequest(HttpConstant.USER_REGISTER, queryParameters: {
      'username': username,
      'password': password,
      'repassword': rePassword,
    }).then((value) => value.data);
  }

  /// 登出
  static logout() async {
    /// 自动移除cookie
    await HttpUtils<String>().getRequest(HttpConstant.USER_LOGOUT);
  }

  /// 获取项目分类
  static Future getProjectTree() {
    return HttpUtils<List<ProjectTreeEntity>>().getRequest(HttpConstant.PROJECT_TREE).then((value) => value.data);
  }

  /// 根据类别获取项目列表
  static Future getProjectList({@required int chapterId, @required int pageId}) {
    String url = "project/list/$pageId/json?cid=$chapterId";
    return HttpUtils<PagingArticleEntity>().getRequest(url).then((value) => value.data?.datas);
  }

  /// 获取分类-->分类列表
  static Future getStructureCategoryList() {
    return HttpUtils<List<StructureCategoryEntity>>()
        .getRequest(HttpConstant.STRUCTURE_CATEGORY)
        .then((value) => value.data);
  }

  /// 获取分类-->网站导航
  static Future getStructureNavList() {
    return HttpUtils<List<StructureNavEntity>>().getRequest(HttpConstant.STRUCTURE_NAV).then((value) => value.data);
  }

  /// 搜索热门记录
  static Future fetchSearchHotKey() {
    return HttpUtils<List<SearchHotKeyEntity>>().getRequest("hotkey/json").then((value) => value.data);
  }

  /// 搜索结果
  static Future fetchSearchResult({key = "", int pageNum = 0}) {
    String urlString = 'article/query/$pageNum/json';

    return HttpUtils<PagingArticleEntity>().postRequest(urlString, queryParameters: {
      'k': key,
    }).then((value) => value.data?.datas);
  }
}
